// pages/profile/star/star.js
import {
  getStarsByUserIdAPI
} from '../../../api/star'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,
    productList: [],
    product: {
      value: 'all',
      options: [{
        value: 'all',
        label: '最近收藏',
      }, ],
    },
    sorter: {
      value: 'default',
      options: [{
          value: 'default',
          label: '默认排序',
        },
        {
          value: 'priceH',
          label: '价格从低到高',
        },
        {
          value: 'priceL',
          label: '价格从高到低',
        },
      ],
    },
  },
  async getStarsByUserId(userId) {
    let that = this
    getStarsByUserIdAPI(userId).then(res => {
      if (res.code == 200) {
        that.setData({
          productList: res.result
        })
      }
    })
  },
  jumpDetail(event) {
    const id = event.currentTarget.id;
    // switchTab tabBar页面跳转，navigateTo 非 tabBar页面跳转
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}`,
    });
  },
  onChange(e) {
    console.log(e.detail.value);
    this.setData({
      'sorter.value': e.detail.value,
    });
    if (e.detail.value == 'priceH') {
      let arrH = this.data.productList.sort((a, b) => {
        return a.price - b.price
      })
      console.log(arrH);
      this.setData({
        productList: arrH
      });
    } else if (e.detail.value == 'priceL') {
      let arrL = this.data.productList.sort((a, b) => {
        return b.price - a.price
      })
      this.setData({
        productList: arrL
      });
    } else {
      this.getStarsByUserId(this.data.userId)
    }
  },
  onLoad(options) {
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        userId: user.id,
      })
    }
    this.getStarsByUserId(this.data.userId)
  },
  onShow() {
    this.onLoad()
  },
})